package com.pai4j.pgc.pojo.convert;
import com.pai4j.common.enums.community.ResourcePathEnum;
import com.pai4j.common.enums.community.ResourceTypeEnum;
import com.pai4j.pgc.entity.community.ResourceEntity;
import com.pai4j.pgc.entity.community.UserEntity;
import com.pai4j.pgc.pojo.es.DtoRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @author: CYM-pai
 * @date: 2025/10/31 12:11
 **/

public class EsSyncResourceConvert {

    /***
     * 专题实体转ES对象
     * @return
     */
    public static List<DtoRequest> resourceConvert2EsDTOList(List<ResourceEntity> resourceEntityList) {
        List<DtoRequest> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(resourceEntityList)) {
            return list;
        }
        String resourceType = ResourceTypeEnum.RESOURCE.getType();
        for (ResourceEntity entity : resourceEntityList) {
            String source = builderSource("瑞索斯", "资源", entity.getResourceName(), entity.getDescription(),
                    entity.getContent(), entity.getPath(), ResourcePathEnum.valueOf(entity.getPath()).getPath());
            String author = "瑞索斯";
            String desc = entity.getDescription();
            Date createDate = entity.getCreateDate();
            Date updateDate = entity.getUpdateDate();
            // 这一步非常重要，避免瑞索斯OSS资源泄露
            entity.setResourceUrl(null);
            entity.setCloudCode(null);
            DtoRequest dto = DtoRequest.of(resourceType, entity.getResourceId(), source, 0L, author, desc, createDate, updateDate, entity);
            list.add(dto);
        }
        return list;
    }

    /***
     * 用户实体转ES对象
     * @return
     */
    public static List<DtoRequest> userConvert2EsDTOList(List<UserEntity> userEntityList) {
        List<DtoRequest> list = new ArrayList<>();

        return list;
    }

    private static String builderSource(String ... vals) {
        StringBuilder tmpSuffix = new StringBuilder();
        if (vals != null && vals.length > 0) {
            for (String val : vals) {
                if (StringUtils.isNotBlank(val)) {
                    tmpSuffix.append(",").append(val);
                }
            }
        }
        return tmpSuffix.toString();
    }
}
